package com.jiugang.cardump.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiugang.cardump.bean.InformationBean;
import com.jiugang.cardump.constant.StatusCodeConstant;
import com.jiugang.cardump.entity.Equipment;
import com.jiugang.cardump.exception.SystemExceptionV110;
import com.jiugang.cardump.mapper.EquipmentMapper;
import com.jiugang.cardump.result.ResultInfoV110;
import com.jiugang.cardump.service.EquipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-08-19
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    /**
     * @param equipment
     * @return Integer
     * @Description 添加一台设备
     */
    @Override
    public ResultInfoV110 insertEquipment(Equipment equipment) {
        log.debug("EquipmentServiceImpl的insertEquipment方法进入,参数equipment为" + equipment);
        //添加一台设备
        Integer insertEquipmentCount = equipmentMapper.insert(equipment);

        if (insertEquipmentCount != 1) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加设备"));
        }

        return ResultInfoV110.success();
    }

    /**
     * @param id
     * @param stuatu
     * @return ResultInfoV110
     * @Description 根据设备编号修改设备状态
     */
    @Override
    public ResultInfoV110 updateEquipmentStatu(String id, Integer stuatu) {
        log.debug("EquipmentServiceImpl的updateEquipmentStatu方法进入,参数id为" + id + ",参数statu为" + stuatu);

        //根据设备编号修改设备状态
        UpdateWrapper<Equipment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("statu", stuatu);
        Integer updateCount = equipmentMapper.update(null, updateWrapper);
        if (updateCount != 1) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "更新设备状态"));
        }
        return ResultInfoV110.success();
    }

    /**
     * @param id
     * @return ResultInfoV110
     * @Description 根据设备编号查询设备信息
     */
    @Override
    public ResultInfoV110 selectEquipmentById(String id) {
        log.debug("EquipmentServiceImpl的selectEquipmentById方法进入,参数id为" + id);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Equipment equipment = equipmentMapper.selectOne(queryWrapper);
        return ResultInfoV110.success(equipment);
    }

    /**
     * @param id
     * @return ResultInfoV110
     * @Description 修改设备信息
     */
    @Override
    public ResultInfoV110 updateEquipment(Equipment equipment) {
        log.debug("EquipmentServiceImpl的updateEquipment方法进入,参数equipment为" + equipment);
        Integer updateCount = equipmentMapper.updateById(equipment);
        if (updateCount != 1) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "修改设备信息"));
        }
        return ResultInfoV110.success();
    }

    /**
     * @param equipment
     * @return ResultInfoV110
     * @Description 根据条件检索所有的设备
     */
    @Override
    public ResultInfoV110 selectAllEquipmentByCondition(Equipment equipment) {
        log.debug("EquipmentServiceImpl的selectAllEquipmentByCondition方法进入,参数equipment为" + equipment);
        //根据条件检索所有的设备
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like((equipment.getId() != null), "id", equipment.getId()).
                like((equipment.getEquipmentName() != null), "equipment_name", equipment.getEquipmentName())
                .eq((equipment.getStatu() != null), "statu", equipment.getStatu())
                .orderByDesc("datetime");

        List<Equipment> equipmentList = equipmentMapper.selectList(queryWrapper);
        return ResultInfoV110.success(equipmentList);
    }

    /**
     * @param equipmentId
     * @return ResultInfoV110
     * @Description 根据设备编号删除设备
     */
    @Override
    public ResultInfoV110 deleteEquipmentById(String equipmentId) {
        log.debug("EquipmentServiceImpl的deleteEquipmentById方法进入,参数equipmentId为" + equipmentId);
        //根据设备编号删除设备
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", equipmentId);
        //结果校验
        Integer deleteCount = equipmentMapper.deleteById(equipmentId);
        if (deleteCount != 1) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "删除设备"));
        }
        return ResultInfoV110.success();
    }


}
